import lejos.nxt.*;
import lejos.robotics.*;
import lejos.robotics.Colors.Color;

/**
 * Simple Java class to use with the SumoEyes infrared sensor for the LEGO NXT
 * @author ISEN
 * @version 1.0
 */
public class EyesSensor implements LampLightDetector, SensorConstants {

    public enum ObstacleDetection {LEFT, CENTER, RIGHT, NO_DETECTION}
    public enum Range {SHORT_RANGE, LONG_RANGE}

    private ADSensorPort port;
    private Range range;




    /**
     * Default constructor
     * @param port sensor port
     */
    public EyesSensor(ADSensorPort port) {
        this.port = port;
        setRange(Range.LONG_RANGE);
        this.port.setTypeAndMode(TYPE_LIGHT_ACTIVE,MODE_PCTFULLSCALE);
    }

    /**
     * Constructor with a range
     * @param port sensor port
     */
    public EyesSensor(ADSensorPort port, Range rangeValue) {
        this.port = port;
        setRange(rangeValue);
        this.port.setTypeAndMode(TYPE_LIGHT_ACTIVE,MODE_PCTFULLSCALE);
    }

    /**
     * Gets the direct value on the port
     * @return the value of the port
     */
    public int getValue() {
        return port.readRawValue();
    }

    /**
     * Gets the direct value on the port
     * @return the value of the port
     */
    public int getDistance() {
        return port.readRawValue();
    }

    /**
     * Returns the detected zone
     * @return detected zone
     */
    public ObstacleDetection getZona(int value) {
//        int value = port.readRawValue();
        if (value == 1023) {
            return ObstacleDetection.NO_DETECTION;
        }
        if (value > 300 && value < 400) {
            return ObstacleDetection.CENTER;
        }
        if (value > 600) {
            return ObstacleDetection.LEFT;
        }
        return ObstacleDetection.RIGHT;
    }

    /**
     * Allows to set the range of the sensor
     * @param range
     */
    public void setRange(Range range) {
        this.range =range;
        switch (range) {
            case SHORT_RANGE: {
                port.setTypeAndMode(TYPE_LIGHT_ACTIVE, MODE_PCTFULLSCALE);
                break;
            }
            case LONG_RANGE: {
                port.setTypeAndMode(TYPE_LIGHT_INACTIVE, MODE_PCTFULLSCALE);
                break;
            }
        }
    }

    /**
     * Returns the current range of the sensor
     * @return current range of the sensor
     */
    public Range getRange() {
        return range;
    }

    /**
     * Permet de changer la distance de d�tection
     * @param largeZoneDetection valeur � true pour d�tection longue distance
     *        et � false pour d�tection courte distance
     */
    public void setWideZoneDetection(boolean largeZoneDetection) {
        if(largeZoneDetection)
            port.setTypeAndMode(TYPE_LIGHT_ACTIVE,MODE_PCTFULLSCALE);
        else
            port.setTypeAndMode(TYPE_LIGHT_INACTIVE,MODE_PCTFULLSCALE);
    }

    // Inherited methods not yest supported
    public boolean isFloodlightOn() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean setFloodlight(Color arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getNormalizedLightValue() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getHigh() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getLow() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Colors.Color getFloodlight() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setFloodlight(boolean bln) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

     public int getLightValue() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
